#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace WaveTech.GShield.Data
{
	public static class ExceptionsDAO
	{
		#region Get All Exception Names

		public static List<string> GetAllExceptionNames()
		{
			var exeptions = from e in Database.DC.Exceptions
			                select e.Name;

			return exeptions.ToList();
		}

		#endregion Get All Exception Names

		public static Exception GetExceptionByName(string name)
		{
			var exeption = from e in Database.DC.Exceptions
			               where e.Name == name
			               select e;

			return exeption.Single();
		}

		public static void Insert(string name)
		{
			Exception e = new Exception();
			e.Name = name;
			e.DateAdded = DateTime.Now;

			Database.DC.Exceptions.InsertOnSubmit(e);
			Database.DC.SubmitChanges();
		}

		public static void Delete(string name)
		{
			var exeption = from e in Database.DC.Exceptions
			               where e.Name == name
			               select e;

			Database.DC.Exceptions.DeleteAllOnSubmit(exeption);
			Database.DC.SubmitChanges();
		}
	}

	#region Old ExceptionsDAO

	/*
  /// <summary>
  /// Exceptions Data Access Objects
  /// </summary>
  public class ExceptionsDAO : DAOBase
  {
    #region Get Exception Names
    /// <summary>
    /// Returns all the process exception names from the database.
    /// </summary>
    /// <returns>ArrayList of process names as strings to not monitor.</returns>
    public ArrayList GetExceptionNames()
    {
      ArrayList l = new ArrayList();
      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT [Name]
																						FROM dbo.Exceptions");

      DataSet ds = db.ExecuteDataSet(dbCommand);

      foreach (DataRow r in ds.Tables[0].Rows)
      {
        l.Add(Encryption.Decrypt(r[0].ToString()));
      }

      return l;
    }
    #endregion Get Exception Names

    #region Get Exceptions
    /// <summary>
    /// Returns all the process exception names and their ID's from the database.
    /// </summary>
    /// <returns>SortedList of process exception names and their ID's</returns>
    public SortedList GetExceptions()
    {
      SortedList l = new SortedList();
      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT [Id], [Name]
																						FROM dbo.Exceptions");

      DataSet ds = db.ExecuteDataSet(dbCommand);

      foreach (DataRow r in ds.Tables[0].Rows)
      {
        l.Add(r[0], Encryption.Decrypt(r[1].ToString()));
      }

      return l;
    }
    #endregion Get Exceptions

		#region Get Exception By Name
		/// <summary>
		/// Returns the process exception Id by it's name from the database.
		/// </summary>
		/// <param name="name"></param>
		/// <returns>Process exception Id if found, else null</returns>
		public int? GetExceptionByName(string name)
		{
			Object retVal;
			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand("SELECT [Id] FROM Exceptions WHERE ([Name] = @Name)");
			db.AddInParameter(dbCommand, "@Name", DbType.String, Encryption.Encrypt(name));

			retVal = db.ExecuteScalar(dbCommand);

			if (retVal != null)
				return (int)retVal;
			else
				return null;
		}
		#endregion Get Exception By Name

		#region Delete
		/// <summary>
    /// Removes a Process Exception from the database by it's ID.
    /// </summary>
    /// <param name="id">Identifier of the Exception to delete.</param>
    public void Delete(int? id)
    {
      if (id == null)
        throw new ArgumentNullException("id");

      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"DELETE
																						FROM Exceptions
																						WHERE (Id = @Id)");

      db.AddInParameter(dbCommand, "@Id", DbType.Int32, id);

      db.ExecuteNonQuery(dbCommand);
    }

    /// <summary>
    /// Removes a Process Exception from the database by it's Name.
    /// </summary>
    /// <param name="name">Name of the Exception to delete.</param>
    public void Delete(string name)
    {
      if (name == null)
        throw new ArgumentNullException("name");

      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"DELETE
																						FROM Exceptions
																						WHERE ([Name] = @Name)");

      db.AddInParameter(dbCommand, "@Name", DbType.String, Encryption.Encrypt(name));

      db.ExecuteNonQuery(dbCommand);
    }
    #endregion Delete

    #region Insert
    /// <summary>
    /// Inserts a new Process Exception into the database
    /// </summary>
    /// <param name="name">Name of the Exception to add</param>
    /// <returns>The Id number of the newly Inserted Exception</returns>
    public int? Insert(string name)
    {
      if (name == null || name.Length <= 0)
        throw new ArgumentNullException("name");
        
      Database db;
      DbCommand dbCommand;
      int? Id;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"INSERT INTO Exceptions 
																						([Name]) VALUES (@Name)
																					SELECT Scope_Identity()");

      db.AddInParameter(dbCommand, "@Name", DbType.String, Encryption.Encrypt(name));

			Id = int.Parse(db.ExecuteScalar(dbCommand).ToString());

      return Id;
    }
    #endregion Insert
  }
    */

	#endregion Old ExceptionsDAO
}